//获取应用实例
const app = getApp();
var falling_time = '';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    open: '',
    phone: '', //输入电话号码
    isimg: 0,
    animationImg: {},
    animationCom: {},//圣旨动画
    user_receive: false,
    model: false,
  },
  formSubmit: function (e) {

    var that = this;
    var value = e.detail.value.phon;
    that.setData({
      phone_value: value,
    })
    if ((/^1[34578]\d{9}$/.test(value))) {
      wx.request({
        url: app.url + 'api_reduce_money/check_mobile',
        data: {
          mobile: value,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {

          console.log(value);
          console.log(res.data);
          var state = res.data.result;
          // state 1:通过验证 2:该电话号码已经被使用 3:验证失败 4:电话号码为空 5:短信发送成功 6 请先去验证 7没有活动 8 活动已过期 9领取失败 10请先完成活动  11 来迟了 12你已经领取了
          if (state == 1) {//1:通过验证
            Confirmation(that);
          } else if (state == 2) {//2:该电话号码已经被使用
            wx.request({
              url: app.url + 'api_reduce_money/send_sms',
              data: {
                mobile: that.data.phone_value,
              },
              header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
              }, success: function (res) {
                console.log('66666');
                console.log(res.data);
                //3:验证失败  5:短信发送成功 
                if (res.data.result == 5) {
                  that.setData({
                    model: true,
                    time: 60,
                  })
                  that.onShow();
                } else {
                  prompt('抱歉，验证失败，请稍后重试');
                }

              }
            })
          } else if (state == 3) {//3:验证失败
            prompt('抱歉，您暂时不是骆驼的会员哦~')
          } else if (state == 4) {//4:电话号码为空
            prompt('抱歉，电话号码不能为空')
          } else if (state == 5) {//5:短信发送成功
            console.log('短信发送成功');
            // prompt('抱歉，电话号码不能为空')
          } else if (state == 6) {//6:请先去验证
            prompt('抱歉，请先输入验证码')
          } else if (state == 7) {//7:没有活动
            prompt('抱歉，暂无活动')
          } else if (state == 8) {//8:活动已过期
            prompt('抱歉，该活动已过期')
          } else if (state == 9) {//9:领取失败
            prompt('抱歉，验证失败')
          } else if (state == 10) {//10:请先完成活动
            prompt('请先完成活动')
          } else if (state == 11) {//11:来迟了
            prompt('抱歉，您来迟了，活动已结束')
          } else if (state == 12) {//12:你已经领取了
            prompt('抱歉，您已经领取过了，请勿重复领取')
          }
          // 提示框
          function prompt(text) {
            wx.showModal({
              title: '提示',
              content: text,
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请正确输入号码',  //标题  
        icon: 'loading',
        duration: 1000,
      })
    }
  },
  delest: function () {
    var that = this;
    that.setData({
      model: false,
    })
    clearInterval(falling_time);
    falling_time = '';
  },
  // 验证码弹出框
  certificationSubmit: function (e) {
    var that = this;
    var index = e.detail.target.dataset.index;
    var text = e.detail.value.certification_text; // 验证码

    if (index == 1) {// 取消弹框
      that.delest();
    } else if (index == 2 && that.data.time == 0) {//重发短信验证码
      wx.request({
        url: app.url + 'api_reduce_money/send_sms',
        data: {
          mobile: that.data.phone_value,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          if (res.data.result == 5) {
            that.setData({
              model: true,
              time: 60,
            })
            that.onShow();
          } else {
            wx.showModal({
              title: '提示',
              content: '抱歉，验证失败，请稍后重试',
            })
          }
        }
      });
    } else if (index == 3) {//确定验证码
      console.log(text);

      if (text != '') {
        wx.request({
          url: app.url + 'api_reduce_money/confirm_sms',
          data: {
            mobile: that.data.phone_value,
            code: text,
          },
          header: {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token_key')
          }, success: function (res) {
            console.log(res);
            if (res.data.result == 1) {
              that.setData({
                model: false,
              })
              clearInterval(falling_time);
              falling_time = '';

              Confirmation(that);
            } else {
              wx.showToast({
                title: '验证码错误',
                icon: 'loading',
                duration: 1500,
              })
            }
          }
        });
      } else {
        wx.showToast({
          title: '请输入验证码',
          icon: 'loading',
          duration: 1500,
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    var that = this;

    // 圣旨初始化
    var animation = wx.createAnimation({ duration: 0, });
    that.animation = animation;
    animation.scale(0, 0).rotate(90).step();

    that.setData({
      id: options.id,
      animationCom: animation.export()
    })


    wx.request({
      url: app.url + 'api_reduce_money/check_mobile',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key'),
      },
      success: function (res) {
        if (res.data.message) {//验证成功，无须重复验证
          wx.redirectTo({
            url: '/pages/reducemoney/money/index?id=' + options.id,
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    //圣旨动画
    var animationCom = wx.createAnimation({ duration: 1000, delay: 500 });
    that.animation = animationCom;
    animationCom.scale(1, 1).rotate(0).step();
    var Intimacy_width = 72 + '%';
    animationCom.width(Intimacy_width).step();


    //圣旨文字动画
    var animationText = wx.createAnimation({ duration: 1000, delay: 3000 });
    that.animation = animationText;
    animationText.opacity(1).step();


    that.setData({
      animationCom: animationCom.export(),
      animationText: animationText.export(),
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var time = that.data.time;
    console.log(time);


    if (time != undefined) {
      falling_time = setInterval(function () {
        if (time > 0) {
          time--;
          if (time == 0) {
            clearInterval(falling_time);
            falling_time = '';
          }
          that.setData({
            time: time,
          })
        } else {
          clearInterval(falling_time);
          falling_time = '';
        }
      }, 1000);
    } else {
      clearInterval(falling_time);
      falling_time = '';
    }
  },
})

function Confirmation(that) {
  console.log(that.data);
  wx.showToast({
    title: '认证成功',  //标题  
    icon: 'success',
    duration: 1000,
  })
  //圣旨文字动画
  var animationText = wx.createAnimation({ duration: 500, });
  that.animation = animationText;
  animationText.opacity(0).step();

  //圣旨动画
  var animationCom = wx.createAnimation({ duration: 1000, delay: 500 });
  that.animation = animationCom;
  var Intimacy_width = 3 + '%';
  animationCom.width(Intimacy_width).step();
  animationCom.scale(0, 0).rotate(90).step();

  that.setData({
    animationText: animationText.export(),
    animationCom: animationCom.export(),
  })
  setTimeout(function () {
    that.setData({
      open: 1,
      isimg: 1,
      user_receive: true,
    })
  }, 3000)
  setTimeout(function () {
    wx.redirectTo({
      url: '/pages/reducemoney/money/index?id=' + that.data.id,
    })
  }, 6500)
};